requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        a: 'manager/listEscrow.do',
        b: 'manager/insertEscrow.do',
        c: 'manager/deleteEscrow.do',
        d: 'manager/updateEscrow.do'
    }
    var frontData = {
        pageNum: 1
    }
    var addData = {};
    var delectData = {};
    var updataData = {};
    var state;

    // 初始化数据
    function init() {
        site.send({
            url: url.a,
            //async:false
            data: frontData,
            callback: function (data) {
                var list = JSON.parse(data)
                var template = Handlebars.compile($("#escrowSet_content_tpl").html());
                $("#escrowSet_content").html(template(list));

                $('#total').text(list.content.total);
                // 分页
                $('.page-container-escrow').createPager({
                    current: frontData.pageNum,
                    total: list.content.total,
                    pageSize: list.content.pageSize,
                    needInput: false,
                    backFn: function (current) {
                        frontData.pageNum = current;
                        init();
                    }
                })
                tr_click();
            }
        })


    }

    init();


    $(".add_commit").on('click', function () {
        addData.mangagementName = $("#inputNames").val();
        addData.remark = $('.add_content').val();
        updataData.mangagementName = $("#inputNames").val();
        updataData.remark = $('.add_content').val();
        if (state == 'add') {
            addFun();
        }
        if (state == 'edit') {
            updataFun();
        }
        event.stopPropagation();

    })


    function addFun() {
        site.send({
            url: url.b,
            //async:false
            data: addData,
            callback: function (data) {
                if (data.msg == '系统异常') {
                    Constant.fn.Message('F', data.msg)
                }
                var list = JSON.parse(data)
                if (list.type == '1') {
                    $('.addwinBox').hide();
                    init();
                } else {
                    Constant.fn.Message('F', list.msg);
                }


            }
        })
    }


    $("#delect_commit").on('click', function () {

        delectFun();
        event.stopPropagation();

    })

    function delectFun() {
        site.send({
            url: url.c,
            //async:false
            data: delectData,
            callback: function (data) {
                if (data.msg == '系统异常') {
                    Constant.fn.Message('F', data.msg)
                }
                var list = JSON.parse(data)
                if (list.type == '1') {
                    $('.delectwinBox').hide();
                    Constant.fn.Message('T', list.msg);
                    init();

                } else {
                    Constant.fn.Message('F', list.msg);
                }


            }
        })
    }

    function updataFun() {
        site.send({
            url: url.d,
            //async:false
            data: updataData,
            callback: function (data) {
                if (data.msg == '系统异常') {
                    Constant.fn.Message('F', data.msg)
                }
                var list = JSON.parse(data)
                if (list.type == '1') {
                    Constant.fn.Message('T', list.msg);
                    $('.addwinBox').hide();
                    init();
                } else {
                    Constant.fn.Message('F', list.msg);
                }


            }
        })
    }


    // 批量操作
    $(".all_delectList").on('change', function () {
        var val = $(this).find("option:selected").val();
        // 批量删除
        if (val == 1) {
            delectData.id = '';
            var inp = $("input[name='ch_id']");//获得所有check按钮
            var count = 0;//计数
            for (var i = 0; i < inp.length; i++) {
                if (inp[i].checked) {
                    count++;
                    if (count < 2) {
                        delectData.id = inp[i].value;
                    } else {
                        delectData.id += ("," + inp[i].value);
                    }
                }
            }
            if (count != 0) {
                delectFun();
            } else {
                Constant.fn.Message("I", "未选择")
            }


            $(".all_delectList").val('-1')
        } // 批量禁用

    })
    //tr点击事件table-focus
    function tr_click(){
        $("tbody tr").on('click',function(){
            if($(this).children(':first-child').children('input').is(':checked')){
                $(this).children(':first-child').children('input').prop('checked',false);
                $(this).removeClass('table-focus')
            }else{
                $(this).addClass('table-focus')
                $(this).children(':first-child').children('input').prop('checked',true);
            }
        })
        //拿到所有的复选框，当有复选框没选上的时候,取消全选
        $("input:checkbox[name='ch_id']").on('click', function () {
            var checkboxs = $("input:checkbox[name='ch_id']");
            var sum = 0;
            var length = checkboxs.length;
            $.each(checkboxs, function (i, checkbox) {
                if ($(checkbox).prop("checked")) {
                    sum++;
                    // 当checked 为true时tr addclass table-focus
                    $(this).parent('td').parent('tr').addClass('table-focus')
                }else{
                    // 当checked 为true时tr removeclass table-focus
                    $(this).parent('td').parent('tr').removeClass('table-focus')
                }
            })
            if (sum == length) {
                $('#checkAllOrNot').prop("checked", true);
            } else {
                $('#checkAllOrNot').prop("checked", false);
            }
            event.stopPropagation();

        })
        //全选和全不选
        $('#checkAllOrNot').on('click', function () {
            $("input[name='ch_id']").prop("checked", $(this).prop("checked"));
            // 当 checked 为true时tr addclass table-focus
            $("input:checkbox[name='ch_id']").each(function(){
                if($(this).prop("checked")){
                    $(this).parent('td').parent('tr').addClass('table-focus')
                }else{
                    $(this).parent('td').parent('tr').removeClass('table-focus')
                }
            })
            event.stopPropagation();

        })
        //删除
        $('.delectBtn').on('click', function () {
            delectData.id = $(this).data('id');
            $(".delectwinBox").show();
            event.stopPropagation();

        })
        //编辑
        $('.editBtn').on('click', function () {
            $("#inputNames").val('');
            $('.add_content').val('');
            state = 'edit';
            $('.addAndEdit_title').text('编辑资方')
            $("#inputNames").val($(this).data('conp'));
            $('.add_content').val($(this).data('remarks'));
            updataData.id=$(this).data('id');
            $('.addwinBox').show();
            event.stopPropagation();

        })
        // 添加
        $(".addBut").on('click', function () {
            $("#inputNames").val('');
            $('.add_content').val('');
            state = 'add';
            $('.addAndEdit_title').text('新增资方')
            $('.addwinBox').show();
            event.stopPropagation();

        })
    }
})